class Solution:
    def romanToInt(self, s: str) -> int:
        RN_1 = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
        RN_2 = {'IV':4, 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900, }
        n = 0
        leng = len(s)
        i = 0
        while i < leng:
            if s[i:i+2] in RN_2:
                n += RN_2[s[i:i+2]]
                i += 2
            else:
                n += RN_1[s[i]]
                i += 1
        return n
        
if __name__ == '__main__':
    s = Solution()
    print(s.romanToInt('MCMXCIV'))
    print(s.romanToInt('LVIII'))
    print(s.romanToInt('IX'))